package com.app.utils;

import com.core.lib.utils.CallBack;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

public class PlaySoundUtil {
	
	private static PlaySoundUtil mInstance = null;
	private MediaPlayer mMediaplayer = null;
	
	private PlaySoundUtil()
	{
		
	}
	
	public static PlaySoundUtil getInstance()
	{
		if(mInstance == null)
		{
			mInstance = new PlaySoundUtil();
		}
		return mInstance;
	}
	
	public void play(Context context, int playId,final boolean isCycle,CallBack callBack)
	{
		mMediaplayer = MediaPlayer.create(context, playId);
		mMediaplayer.setLooping(isCycle);
		
		if(mMediaplayer != null)
		{
			mMediaplayer.start();
			mMediaplayer.setOnCompletionListener(new OnCompletionListener() {
				
				@Override
				public void onCompletion(MediaPlayer mp) {
					//
				}
			});
			
			if(callBack != null)
			{
				callBack.callBackSuccess(null);
			}
		}
	}
	
	public void stop(CallBack callBack)
	{
		try {
			if (mMediaplayer != null && mMediaplayer.isPlaying()) {
				mMediaplayer.stop();
				mMediaplayer = null;
			}
			
			if(callBack != null)
			{
				callBack.callBackSuccess(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			
			if(callBack != null)
			{
				callBack.callBackFail(null);
			}
		}
	}
}
